package cn.lili.modules.order.order.manage;

import cn.lili.modules.goods.client.GoodsSkuClient;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.OrderCancel;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import cn.lili.modules.order.order.entity.enums.OrderStatusEnum;
import cn.lili.modules.order.order.entity.enums.PayStatusEnum;
import cn.lili.modules.order.order.service.OrderCancelService;
import cn.lili.modules.order.order.service.OrderItemService;
import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import jodd.util.StringUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单管理类
 */
@Component
public class OrderManage {

    @Resource
    private OrderCancelService orderCancelService;
    @Resource
    private OrderItemService orderItemService;
    @Resource
    private GoodsSkuClient goodsSkuClient;

    /**
     * 处理企叮咚/胜创汇联取消订单请求
     *
     * @param order  订单信息
     * @param reason 原因
     */
    public Boolean qqdCancelled(Order order, String reason,String resultId) {
        if(PayStatusEnum.UNPAID.name().equals(order.getPayStatus())){
            return false;
        }
        //状态如果为取消中，则不进入
        if (StringUtil.isNotEmpty(resultId)) {
            OrderCancel entity = orderCancelService.getBaseMapper().selectById(resultId);
            entity.setType(OrderStatusEnum.CANCELLED.name());
            entity.setTypeName(OrderStatusEnum.CANCELLED.description);
            orderCancelService.updateById(entity);
            return false;
        }

        //查询当前商品是否为企叮咚商品
        List<OrderItem> orderItemList = orderItemService.getByOrderSn(order.getSn());
        boolean flag = false;
        for (OrderItem orderItem : orderItemList) {
            GoodsSku goodsSku = goodsSkuClient.getGoodsSkuById(orderItem.getSkuId());
            if (goodsSku != null && ("2".equals(String.valueOf(goodsSku.getThirdPartyPlatformType())) || "3".equals(String.valueOf(goodsSku.getThirdPartyPlatformType())) )) {
                flag = true;
            }
        }

        //新增取消申请
        if (flag) {
            OrderCancel orderCancel = new OrderCancel();
            orderCancel.setOrderSn(order.getSn());
            orderCancel.setType(OrderStatusEnum.CANCELLED_LODING.name());
            orderCancel.setTypeName(OrderStatusEnum.CANCELLED_LODING.description);
            orderCancel.setReason(reason);
            orderCancel.setStoreId(order.getStoreId());
            orderCancelService.save(orderCancel);
        }
        return flag;
    }

    /**
     *
     * 更新取消状态为拒绝
     */
    public void updateOrderCancelRefuse(String id){
        OrderCancel orderCancel = new OrderCancel();
        orderCancel.setId(id);
        orderCancel.setType("CANCELLED_REFUSE");
        orderCancel.setTypeName("已拒绝");
        orderCancelService.updateById(orderCancel);
    }
}
